package com.sc.smartcommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.smartcommunity.domain.UserAddress;
import com.sc.smartcommunity.mapper.UserAddressMapper;
import com.sc.smartcommunity.service.UserAddressService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Chenke Zhao
 * @date 2025/9/10 19:25
 * @description UserAddressServiceImpl
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService  {

    @Resource
    private UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddress> getAddressList(String loginId) {
        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddress::getPhone,loginId);
        return userAddressMapper.selectList(wrapper);
    }

    @Override
    public boolean addAddress(UserAddress address) {
        return userAddressMapper.insert(address) > 0;
    }

    @Override
    public boolean deleteAddress(Long id) {
        return userAddressMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateAddress(UserAddress address) {
        return userAddressMapper.updateById(address) > 0;
    }

    @Override
    public boolean setDefaultAddress(Long id) {
        LambdaUpdateWrapper<UserAddress> wrapper = new LambdaUpdateWrapper<UserAddress>();
        wrapper.eq(UserAddress::getId,id)
                .set(UserAddress::getIsDefault,1);
        return userAddressMapper.update(null,wrapper) > 0;
    }
}
